{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Package loaded\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "Logistic_Regression with Custom data\n",
    "author hadxu(hadxu123@gmail.com)\n",
    "\"\"\"\n",
    "import tensorflow as tf\n",
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "print 'Package loaded'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "69 2 900 18\n"
     ]
    }
   ],
   "source": [
    "cwd = os.getcwd()\n",
    "loadpath = cwd+'/data/trainingset.npz'\n",
    "l = np.load(loadpath)\n",
    "l.files\n",
    "\n",
    "trainimg = l['trainimg']\n",
    "trainlabel = l['trainlabel']\n",
    "testimg = l['testimg']\n",
    "testlabel = l['testlabel']\n",
    "ntrain = trainimg.shape[0]\n",
    "nclass = trainlabel.shape[1]\n",
    "dim = trainimg.shape[1]\n",
    "ntest = testimg.shape[0]\n",
    "print ntrain,nclass,dim,ntest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ready\n"
     ]
    }
   ],
   "source": [
    "#Parameters of Logistic Regression\n",
    "learning_rate = 0.001\n",
    "training_epochs = 1000\n",
    "batch_size = 10\n",
    "display_step = 10\n",
    "x = tf.placeholder('float',[None,dim])\n",
    "y = tf.placeholder('float',[None,nclass])\n",
    "W = tf.Variable(tf.zeros([dim,nclass]))\n",
    "b = tf.Variable(tf.zeros([nclass]))\n",
    "_pred = tf.nn.softmax(tf.matmul(x,W)+b)\n",
    "#cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(_pred),reduction_indices=1))\n",
    "\n",
    "cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(_pred)+(1-y)*tf.log(1-_pred)))\n",
    "\n",
    "\n",
    "optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)\n",
    "_corr = tf.equal(tf.argmax(_pred,1),tf.argmax(y,1))\n",
    "accr = tf.reduce_mean(tf.cast(_corr,tf.float32))\n",
    "init = tf.initialize_all_variables()\n",
    "print 'Ready'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 000/1000 cost: 15.911776702\n",
      " Training accuracy: 0.800\n",
      " Test accuracy: 0.722\n",
      "Epoch: 010/1000 cost: 23.522638798\n",
      " Training accuracy: 0.800\n",
      " Test accuracy: 0.778\n",
      "Epoch: 020/1000 cost: 12.206444820\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.778\n",
      "Epoch: 030/1000 cost: 11.030219555\n",
      " Training accuracy: 0.900\n",
      " Test accuracy: 0.778\n",
      "Epoch: 040/1000 cost: 4.998107175\n",
      " Training accuracy: 0.900\n",
      " Test accuracy: 0.944\n",
      "Epoch: 050/1000 cost: 6.970496535\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.944\n",
      "Epoch: 060/1000 cost: 2.743720035\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.944\n",
      "Epoch: 070/1000 cost: 2.429382205\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 080/1000 cost: 2.536454101\n",
      " Training accuracy: 0.900\n",
      " Test accuracy: 0.444\n",
      "Epoch: 090/1000 cost: 1.691683580\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 100/1000 cost: 1.417197327\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 110/1000 cost: 2.033675075\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 120/1000 cost: 1.938720902\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.833\n",
      "Epoch: 130/1000 cost: 1.139683187\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 140/1000 cost: 1.285841366\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 150/1000 cost: 1.643112739\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 160/1000 cost: 1.407980005\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.833\n",
      "Epoch: 170/1000 cost: 1.135179838\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 1.000\n",
      "Epoch: 180/1000 cost: 1.257215401\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 190/1000 cost: 1.180494150\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 200/1000 cost: 0.846585522\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 210/1000 cost: 1.070168585\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 220/1000 cost: 0.972057462\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 230/1000 cost: 0.950930605\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 240/1000 cost: 1.276890904\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 1.000\n",
      "Epoch: 250/1000 cost: 0.683360974\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 260/1000 cost: 0.813982834\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 270/1000 cost: 0.938855956\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 280/1000 cost: 0.748473406\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 290/1000 cost: 0.685296694\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 300/1000 cost: 0.799212277\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 310/1000 cost: 0.533620884\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 320/1000 cost: 0.503842796\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 330/1000 cost: 0.547672321\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 340/1000 cost: 0.597426996\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 350/1000 cost: 0.591209104\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 360/1000 cost: 0.753197511\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 370/1000 cost: 0.624076476\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 380/1000 cost: 0.780297885\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 390/1000 cost: 0.568513458\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 400/1000 cost: 0.549562693\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 410/1000 cost: 0.622088507\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 420/1000 cost: 0.605526994\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 430/1000 cost: 0.502465114\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 440/1000 cost: 0.548085848\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 450/1000 cost: 0.545169100\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 460/1000 cost: 0.607054392\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 470/1000 cost: 0.526036412\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 480/1000 cost: 0.428614403\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 490/1000 cost: 0.429694444\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 500/1000 cost: 0.568385730\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 510/1000 cost: 0.410196294\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 520/1000 cost: 0.446087728\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 530/1000 cost: 0.500979756\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 540/1000 cost: 0.468410343\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 550/1000 cost: 0.537747324\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 560/1000 cost: 0.383715108\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 570/1000 cost: 0.529523229\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 580/1000 cost: 0.345114127\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 590/1000 cost: 0.355679564\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 600/1000 cost: 0.329683060\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 610/1000 cost: 0.383598417\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 620/1000 cost: 0.479632646\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 630/1000 cost: 0.376633490\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 640/1000 cost: 0.504965176\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 650/1000 cost: 0.382838562\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 660/1000 cost: 0.396143764\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 670/1000 cost: 0.318390449\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 680/1000 cost: 0.361244236\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 690/1000 cost: 0.257581117\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 700/1000 cost: 0.331106270\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 710/1000 cost: 0.313009570\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 720/1000 cost: 0.344054118\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 730/1000 cost: 0.317129098\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 740/1000 cost: 0.327272937\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 750/1000 cost: 0.294158739\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 760/1000 cost: 0.292977879\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 770/1000 cost: 0.325720022\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 780/1000 cost: 0.294308851\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 790/1000 cost: 0.292526464\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 800/1000 cost: 0.321129310\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 810/1000 cost: 0.239129757\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 820/1000 cost: 0.374708071\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 830/1000 cost: 0.247140594\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 840/1000 cost: 0.301872234\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 850/1000 cost: 0.318083805\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 860/1000 cost: 0.345302055\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 870/1000 cost: 0.300441528\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 880/1000 cost: 0.260374481\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 890/1000 cost: 0.249052972\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 900/1000 cost: 0.224987263\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 910/1000 cost: 0.288605504\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 920/1000 cost: 0.284814000\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 930/1000 cost: 0.251994724\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 940/1000 cost: 0.259666237\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 950/1000 cost: 0.253850674\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 960/1000 cost: 0.248749542\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 970/1000 cost: 0.241683275\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 980/1000 cost: 0.269113225\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Epoch: 990/1000 cost: 0.326038336\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.889\n",
      "Optimization Finished!\n"
     ]
    }
   ],
   "source": [
    "# Launch the graph\n",
    "sess = tf.Session()\n",
    "sess.run(init)\n",
    "\n",
    "# Training cycle\n",
    "for epoch in range(training_epochs):\n",
    "    avg_cost = 0.\n",
    "    num_batch = int(ntrain/batch_size)\n",
    "    # Loop over all batches\n",
    "    for i in range(num_batch): \n",
    "        randidx = np.random.randint(ntrain, size=batch_size)\n",
    "        batch_xs = trainimg[randidx, :]\n",
    "        batch_ys = trainlabel[randidx, :]                \n",
    "        # Fit training using batch data\n",
    "        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys})\n",
    "        # Compute average loss\n",
    "        avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/num_batch\n",
    "\n",
    "    # Display logs per epoch step\n",
    "    if epoch % display_step == 0:\n",
    "        print (\"Epoch: %03d/%03d cost: %.9f\" % (epoch, training_epochs, avg_cost))\n",
    "        train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys})\n",
    "        print (\" Training accuracy: %.3f\" % (train_acc))\n",
    "        test_acc = sess.run(accr, feed_dict={x: testimg, y: testlabel})\n",
    "        print (\" Test accuracy: %.3f\" % (test_acc))\n",
    "\n",
    "print (\"Optimization Finished!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
